package vaannila;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String message;
	private String userName;
	private String userID;

	public HelloWorld() {
	}

	public String execute() {

		String ret = "error";

		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;

		try {
			String DB_SERVER = "localhost:3306";
			String DB_USERNAME = "root";
			String DB_PASSWORD = "tkfkdgo";
			String DB_DATABASE = "db_test";
			String DB_TABLE = "member";

			String jdbcUrl = "jdbc:mysql://" + DB_SERVER + "/" + DB_DATABASE;

			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager
					.getConnection(jdbcUrl, DB_USERNAME, DB_PASSWORD);

			String sql = "SELECT * FROM member where";
			sql += " userName = ? AND userID = ?";
			PreparedStatement ps = con.prepareStatement(sql);
			ps.setString(1, userName);
			ps.setString(2, userID);
			rs = ps.executeQuery();

			while (rs.next()) {
				// userName = rs.getString(1);

				ret = "SUCCESS";
			}
		} catch (Exception e) {
			ret = "error";
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (Exception e) {
				}
			}

			if (isInvalid(getUserName()))
				return "error";
			if (isInvalid(getUserID()))
				return "error";

		}

		return ret;

	}

	public String fail() {
		return "fail";
	}

	public String send() {
		return "send";
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message
	 *            the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the userName
	 */
	public String getUserName() {
		return userName;
	}

	/**
	 * @param userName
	 *            the userName to set
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserID() {
		return userID;
	}

	/**
	 * @param userName
	 *            the userName to set
	 */
	public void setUserID(String userID) {
		this.userID = userID;
	}

	private boolean isInvalid(String value) {

		boolean a = (value == null || value.length() == 0);

		return a;
	}

}